using System;
using System.IO;
using System.Threading.Tasks;

namespace Infrastructure.AliyunOSS
{
    /// <summary>
    /// 阿里云OSS服务接口
    /// </summary>
    public interface IAliyunOSSService
    {
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="objectName">对象名称（OSS中的文件路径）</param>
        /// <param name="fileStream">文件流</param>
        /// <param name="contentType">内容类型（如：image/jpeg）</param>
        /// <returns>上传结果</returns>
        Task<AliyunOSSResult> UploadAsync(string objectName, Stream fileStream, string contentType);
        
        /// <summary>
        /// 上传文件到指定存储桶
        /// </summary>
        /// <param name="bucketName">存储桶名称</param>
        /// <param name="objectName">对象名称（OSS中的文件路径）</param>
        /// <param name="fileStream">文件流</param>
        /// <param name="contentType">内容类型（如：image/jpeg）</param>
        /// <returns>上传结果</returns>
        Task<AliyunOSSResult> UploadAsync(string bucketName, string objectName, Stream fileStream, string contentType);
        
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="objectName">对象名称（OSS中的文件路径）</param>
        /// <param name="filePath">本地文件路径</param>
        /// <returns>上传结果</returns>
        Task<AliyunOSSResult> UploadAsync(string objectName, string filePath);
        
        /// <summary>
        /// 上传文件到指定存储桶
        /// </summary>
        /// <param name="bucketName">存储桶名称</param>
        /// <param name="objectName">对象名称（OSS中的文件路径）</param>
        /// <param name="filePath">本地文件路径</param>
        /// <returns>上传结果</returns>
        Task<AliyunOSSResult> UploadAsync(string bucketName, string objectName, string filePath);
        
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="objectName">对象名称（OSS中的文件路径）</param>
        /// <param name="fileBytes">文件字节数组</param>
        /// <param name="contentType">内容类型（如：image/jpeg）</param>
        /// <returns>上传结果</returns>
        Task<AliyunOSSResult> UploadAsync(string objectName, byte[] fileBytes, string contentType);
        
        /// <summary>
        /// 上传文件到指定存储桶
        /// </summary>
        /// <param name="bucketName">存储桶名称</param>
        /// <param name="objectName">对象名称（OSS中的文件路径）</param>
        /// <param name="fileBytes">文件字节数组</param>
        /// <param name="contentType">内容类型（如：image/jpeg）</param>
        /// <returns>上传结果</returns>
        Task<AliyunOSSResult> UploadAsync(string bucketName, string objectName, byte[] fileBytes, string contentType);
        
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="objectName">对象名称（OSS中的文件路径）</param>
        /// <returns>删除结果</returns>
        Task<bool> DeleteAsync(string objectName);
        
        /// <summary>
        /// 从指定存储桶删除文件
        /// </summary>
        /// <param name="bucketName">存储桶名称</param>
        /// <param name="objectName">对象名称（OSS中的文件路径）</param>
        /// <returns>删除结果</returns>
        Task<bool> DeleteAsync(string bucketName, string objectName);
        
        /// <summary>
        /// 获取文件URL
        /// </summary>
        /// <param name="objectName">对象名称（OSS中的文件路径）</param>
        /// <param name="expiry">过期时间（默认1小时）</param>
        /// <returns>访问URL</returns>
        string GetObjectUrl(string objectName, TimeSpan? expiry = null);
        
        /// <summary>
        /// 获取指定存储桶中文件的URL
        /// </summary>
        /// <param name="bucketName">存储桶名称</param>
        /// <param name="objectName">对象名称（OSS中的文件路径）</param>
        /// <param name="expiry">过期时间（默认1小时）</param>
        /// <returns>访问URL</returns>
        string GetObjectUrl(string bucketName, string objectName, TimeSpan? expiry = null);
        
        /// <summary>
        /// 检查文件是否存在
        /// </summary>
        /// <param name="objectName">对象名称（OSS中的文件路径）</param>
        /// <returns>是否存在</returns>
        Task<bool> ExistsAsync(string objectName);
        
        /// <summary>
        /// 检查指定存储桶中文件是否存在
        /// </summary>
        /// <param name="bucketName">存储桶名称</param>
        /// <param name="objectName">对象名称（OSS中的文件路径）</param>
        /// <returns>是否存在</returns>
        Task<bool> ExistsAsync(string bucketName, string objectName);
        
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="objectName">对象名称（OSS中的文件路径）</param>
        /// <returns>文件字节数组</returns>
        Task<byte[]?> DownloadAsync(string objectName);
        
        /// <summary>
        /// 从指定存储桶下载文件
        /// </summary>
        /// <param name="bucketName">存储桶名称</param>
        /// <param name="objectName">对象名称（OSS中的文件路径）</param>
        /// <returns>文件字节数组</returns>
        Task<byte[]?> DownloadAsync(string bucketName, string objectName);
    }
    
    /// <summary>
    /// 阿里云OSS操作结果
    /// </summary>
    public class AliyunOSSResult
    {
        /// <summary>
        /// 是否成功
        /// </summary>
        public bool Success { get; set; }
        
        /// <summary>
        /// 文件URL
        /// </summary>
        public string? Url { get; set; }
        
        /// <summary>
        /// 错误信息
        /// </summary>
        public string? ErrorMessage { get; set; }
        
        /// <summary>
        /// 对象名称（OSS中的文件路径）
        /// </summary>
        public string? ObjectName { get; set; }
        
        /// <summary>
        /// 存储桶名称
        /// </summary>
        public string? BucketName { get; set; }
    }
} 